//
//  SafeProxyTestViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2019/12/31.
//  Copyright © 2019 Xing. All rights reserved.
//

#import "SafeProxyTestViewController.h"
#import "SafeObjectProxy.h"
#import <Masonry.h>

@interface SafeObjectProxy(Report)<SafeObjectReportProtocol>
@end
@implementation SafeObjectProxy(Report)
-(void)reportDefendCrashLog:(NSString *)log {
    NSArray *array=[NSThread callStackSymbols];
    NSLog(@"\n\n\n  =============== crash log begin ===============  ");
    NSLog(@"crash log:%@\n%@",log,array);
    NSLog(@"\n\n\n  =============== crash log end ===============  ");
}
@end

@interface SafeProxyTestViewController ()

proStrong UILabel *exceptionLabel;

@end

@implementation SafeProxyTestViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    UIButton *testBtn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testBtn1.tag = 1;
    testBtn1.titleLabel.font = [UIFont systemFontOfSize:14];
    testBtn1.titleLabel.textColor = [UIColor blueColor];
    [testBtn1 setTitle:@"数组越界" forState:UIControlStateNormal];
    [Utils resetView:testBtn1 bgColor:[UIColor whiteColor] cornerRadius:5 borderWidth:1 borderColor:kColorLineColor];
    [testBtn1 addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:testBtn1];
    
    UIButton *testBtn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testBtn2.tag = 2;
    testBtn2.titleLabel.font = [UIFont systemFontOfSize:14];
    testBtn2.titleLabel.textColor = [UIColor blueColor];
    [testBtn2 setTitle:@"字典插入空" forState:UIControlStateNormal];
    [Utils resetView:testBtn2 bgColor:[UIColor whiteColor] cornerRadius:5 borderWidth:1 borderColor:kColorLineColor];
    [testBtn2 addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:testBtn2];
    
    UIButton *testBtn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testBtn3.tag = 3;
    testBtn3.titleLabel.font = [UIFont systemFontOfSize:14];
    testBtn3.titleLabel.textColor = [UIColor blueColor];
    [testBtn3 setTitle:@"找不到方法" forState:UIControlStateNormal];
    [Utils resetView:testBtn3 bgColor:[UIColor whiteColor] cornerRadius:5 borderWidth:1 borderColor:kColorLineColor];
    [testBtn3 addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:testBtn3];
    
    UIButton *hookBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    hookBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [hookBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [hookBtn setTitleColor:kColorTfPlaceholderColor forState:UIControlStateDisabled];
    [hookBtn addTarget:self action:@selector(hookAction:) forControlEvents:UIControlEventTouchUpInside];
    [hookBtn setTitle:@"安全防范hook" forState:UIControlStateNormal];
    [Utils resetView:hookBtn bgColor:[UIColor whiteColor] cornerRadius:5 borderWidth:1 borderColor:kColorLineColor];
    [self.view addSubview:hookBtn];
    
    self.exceptionLabel = [[UILabel alloc] init];
    self.exceptionLabel.font = [UIFont systemFontOfSize:15];
    self.exceptionLabel.numberOfLines = 0;
    [self.view addSubview:self.exceptionLabel];
    
    [testBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuide).offset(10);
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(44);
    }];
    [testBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(testBtn1.mas_top);
        make.left.equalTo(testBtn1.mas_right).offset(20);
        make.height.mas_equalTo(44);
        make.width.equalTo(testBtn1.mas_width);
    }];
    [testBtn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(testBtn1.mas_top);
        make.left.equalTo(testBtn2.mas_right).offset(20);
        make.height.mas_equalTo(44);
        make.width.equalTo(testBtn2.mas_width);
        make.right.mas_equalTo(-20);
    }];
    
    [hookBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(testBtn1.mas_bottom).offset(20);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(44);
    }];
    
    [self.exceptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(hookBtn.mas_bottom).offset(30);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
}

/// hook方法
/// @param sender 按钮
- (void)hookAction:(UIButton *)sender {
    
    sender.enabled = NO;
    
    [SafeObjectProxy startSafeObjectProxy];
    //[SafeObjectProxy addSafeDanglingPointerClassNames:@[@"SSS"]];
}

/// 测试事件
/// @param sender 按钮
- (void)testAction:(UIButton *)sender {
    
    self.exceptionLabel.text = nil;
    BOOL hasException = NO;
    
    @try {
        
        switch (sender.tag) {
            case 1:
                [self testOutBoundsTestAction];
                break;
            case 2:
                [self testDicInsertNilObjectAction];
                break;
            case 3:
                [self testSelectorNotFoundAction];
                break;
                
            default:
                break;
        }
        
    } @catch (NSException *exception) {
        hasException = YES;
        self.exceptionLabel.text = [NSString stringWithFormat:@"catch到异常：\n%@",exception];
    } @finally {
    }
    
    if (!hasException) {
        self.exceptionLabel.textColor = [UIColor greenColor];
        self.exceptionLabel.text = @"没有catch到异常！";
    } else {
        self.exceptionLabel.textColor = [UIColor redColor];
    }
}

/// 数组越界
- (void)testOutBoundsTestAction {
    
    NSArray *arr=@[@""];
    NSLog(@"%@",[arr objectAtIndex:10]);
    NSLog(@"~~~ %s Done.",__func__);
}

/// 字典插入空
- (void)testDicInsertNilObjectAction {
    
    NSMutableDictionary *dic = @[].mutableCopy;
    NSObject *obj = nil;
    [dic setObject:obj forKey:@"key"];
    NSLog(@"~~~ %s Done.",__func__);
}

/// 找不到方法
- (void)testSelectorNotFoundAction {
    
     [((NSString*)[NSNull null]) substringToIndex:10];
    NSLog(@"~~~ %s Done.",__func__);
}

@end
